package com.atguigu.springcloud.config;

import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

/**
 * Gateway使用编码格式进行路由
 * 代码中注入RouteLocator的Bean
 * 可以了解 动态路由
 */
@Configuration
public class Gateway {

    @Bean
    public RouteLocator routeLocator(RouteLocatorBuilder routeLocatorBuilder){
        RouteLocatorBuilder.Builder routes = routeLocatorBuilder.routes();
//访问 localhost:9527/guoji将会转发到    http://news.baidu.com/guoji
        routes.route("path_route_hyq1",r ->r.path("/guoji").uri("http://news.baidu.com/guoji")).build();

        return  routes.build();
    }
    @Bean
    public RouteLocator routeLocator2(RouteLocatorBuilder routeLocatorBuilder){
        RouteLocatorBuilder.Builder routes = routeLocatorBuilder.routes();
//访问 localhost:9527/guoji将会转发到    http://news.baidu.com/guoji
        routes.route("path_route_hyq2",r ->r.path("/game").uri("http://news.baidu.com/game")).build();

        return  routes.build();
    }

}

